package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.ScrollView

/**
 * 继承ScrollView用来放开滚动监听接口
 * @author ljk
 * @date   2019年10月29日
 */
class ObservableScrollView : ScrollView {
    var scrollDownY = 0
    var downY = 0f
    private var onScrollViewListener: OnScrollViewListener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean { //记录scrollview滚动前，手指按在屏幕上时的Y坐标
        if (event.action == MotionEvent.ACTION_DOWN) {
            downY = event.y
            scrollDownY = this.scrollY
        }
        return super.onInterceptTouchEvent(event)
    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        super.onScrollChanged(l, t, oldl, oldt)
        if (onScrollViewListener != null) {
            onScrollViewListener!!.onScrollChanged(this, l, t, oldl, oldt)
        }
    }

    fun setOnScrollViewListener(onScrollViewListener: OnScrollViewListener?) {
        this.onScrollViewListener = onScrollViewListener
    }

    interface OnScrollViewListener {
        fun onScrollChanged(scrollView: ObservableScrollView?, x: Int, y: Int, oldx: Int, oldy: Int)
    }
}
